package cn.enilu.flash.api.controller.task;

import cn.enilu.flash.api.controller.BaseController;
import cn.enilu.flash.bean.entity.task.Proof;
import cn.enilu.flash.bean.vo.query.SearchFilter;
import cn.enilu.flash.service.task.ProofService;

import cn.enilu.flash.bean.core.BussinessLog;
import cn.enilu.flash.bean.constant.factory.PageFactory;
import cn.enilu.flash.bean.enumeration.BizExceptionEnum;
import cn.enilu.flash.bean.exception.ApplicationException;
import cn.enilu.flash.bean.vo.front.Ret;
import cn.enilu.flash.bean.vo.front.Rets;

import cn.enilu.flash.utils.factory.Page;


import org.apache.shiro.authz.annotation.RequiresPermissions;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/proof")
public class ProofController extends BaseController {
	private  Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private ProofService proofService;

	@GetMapping(value = "/list")
	@RequiresPermissions(value = "proof")
	public Ret list(@RequestParam(required = false) Long id) {
		Page<Proof> page = new PageFactory<Proof>().defaultPage();
		page.addFilter("id",id);
		page = proofService.queryPage(page);
		return Rets.success(page);
	}
	@GetMapping("getProofByTaskId")
	public Ret getProofByTaskId(Long taskId){
		List<Proof> proofList = proofService.queryAll(new SearchFilter("taskId", SearchFilter.Operator.EQ, taskId));
		return Rets.success(proofList);
	}
	@GetMapping("getProofByOrderId")
	public Ret getProofByOrderId(Long orderId){
		List<Proof> proofList = proofService.queryAll(new SearchFilter("orderId", SearchFilter.Operator.EQ, orderId));
		return Rets.success(proofList);
	}
	@PostMapping
	@BussinessLog(value = "新增任务/订单凭证", key = "name")
	@RequiresPermissions(value = "proofAdd")
	public Ret add(@ModelAttribute Proof proof){
		proofService.insert(proof);
		return Rets.success();
	}
	@PutMapping
	@BussinessLog(value = "更新任务/订单凭证", key = "name")
	@RequiresPermissions(value = "proofUpdate")
	public Ret update(@ModelAttribute Proof proof){
		proofService.update(proof);
		return Rets.success();
	}
	@DeleteMapping
	@BussinessLog(value = "删除任务/订单凭证", key = "id")
	@RequiresPermissions(value = "proofDelete")
	public Ret remove(Long id){
		if (id == null) {
			throw new ApplicationException(BizExceptionEnum.REQUEST_NULL);
		}
		proofService.delete(id);
		return Rets.success();
	}
}